package com.meringer.beans.ejb;

import java.security.Principal;

import javax.annotation.Resource;
import javax.ejb.LocalBean;
import javax.ejb.SessionContext;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.meringer.entities.User;

/* An EJB representing the current user
 * Will be called only by the UserBean which is a managed bean
 * called by the JSF pages
 */

@Stateful
@LocalBean

public class UserEJB {
	
	@PersistenceContext
	private EntityManager em;
	@Resource
	SessionContext ctx;
	
	private User user;
	
    public UserEJB() {
    	
    }
    
    public User currentUser(){
//    	if(user == null){
	    	Principal callerPrincipal = ctx.getCallerPrincipal();
	    	String userName = callerPrincipal.getName();
	    	user = em.find(User.class, userName);
	    	em.refresh(user);
//    	}
//    	System.out.print(user.getImgPath());
    	return user;
    }
    public void updateCurrentUser(User user){
    	System.out.println(user.getImgPath());
    	this.user = user;
    	em.merge(user);
    	System.out.println(user.getImgPath());
    }
    

}
